package edu.java.water.service.impl;

import edu.java.water.entity.CartEntity;
import edu.java.water.mapper.CartMapper;
import edu.java.water.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    CartMapper cartMapper;
    @Override
    public List<CartEntity> selectCart(Integer user_id) {
        return cartMapper.selectCart(user_id);
    }

    @Override
    public Boolean setCartNumber(Integer product_num, Integer id) {
        int row=0;
        if(product_num>0){
            row = cartMapper.setCartNumber(product_num,id);
        }else{
            row =cartMapper.deleteCartNumber(id);
        }
        if(row>0){
            return true;
        }
        return false;
    }

    @Override
    public Integer setOrder(Integer user_id, String[] cart_ids) {
        try {
            List<String> list = new ArrayList<>();
            for (String cart_id:cart_ids){
                list.add(cart_id);
            }
            List<CartEntity> cartEntities = cartMapper.setOrder(user_id, (ArrayList<String>) list);
            if(cartEntities.size()==list.size()){
                return 1;
            }else {
                return -1;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return 0;
    }
}
